package com.summer.main;

import java.util.ArrayList;
import java.util.Arrays;

import com.carrotsearch.sizeof.RamUsageEstimator;

public class ByteTest {
	
//	final static int length = 1024*1024*100;
	final static int length = 1024*1024*1;
	
	public static void main(String[] args) {
		String[] strArray = new String[length];
		byte[] byteArray = new byte[length];
		
		Arrays.fill(strArray, "a");
		Arrays.fill(byteArray, (byte)'a');
		
		strArray[length-10] = "b";
		byteArray[length-10] = (byte)'b';
		
		long time1 = System.currentTimeMillis();
		
		for(String str : strArray) {
			if("b" == str) {
				System.out.println(String.format("String 找到了:%s,耗时：%s", str, (System.currentTimeMillis() - time1)));
			}
		}
		

		long time2 = System.currentTimeMillis();
		
		for(byte by : byteArray) {
			if('b' == by) {
				System.out.println(String.format("byte 找到了:%s,耗时：%s", (char)by, (System.currentTimeMillis() - time2)));
			}
		}
		
		System.out.println(String.format("byte 大小：%s, String 大小：%s", 
				RamUsageEstimator.sizeOf(byteArray),  
				RamUsageEstimator.sizeOf(strArray)
				));
		
		ArrayList<String> strList = new ArrayList<String>();
		ArrayList<byte[]> byList = new ArrayList<byte[]>();
		
		for(int i=0;i<length;i++) {
			strList.add("aaa");
			byList.add("aaa".getBytes());
		}
		
		strList.add(length - 100, "bbb");
		byte[] src = "bbb".getBytes();
		byList.add(length - 100, src);
		
		long time3 = System.currentTimeMillis();
		for(String str : strList) {
			if("bbb" == str) {
				System.out.println(String.format("String 找到了:%s,耗时：%s", str, (System.currentTimeMillis() - time3)));
			}
		}
		
		
		long time4 = System.currentTimeMillis();
		for(byte[] by : byList) {
			if(src == by) {
				System.out.println(String.format("byte 找到了:%s,耗时：%s", String.valueOf(src), (System.currentTimeMillis() - time4)));
			}
		}

		System.out.println(String.format("byte 大小：%s, String 大小：%s", 
				RamUsageEstimator.sizeOf(byList),
				RamUsageEstimator.sizeOf(strList)
				));
		
	}
	
}
